package NetCrossServer.server;

import NetCrossServer.thread.ServerThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class TcpServer {
    private static Logger logger = LoggerFactory.getLogger(UdpServer.class);
    public static List<ServerThread> st = new ArrayList<>();
    private String ip;
    private int port;
    private static int count = 0;

    public TcpServer(int port) {
        this.port = port;
    }

    public TcpServer() {
    }

    public void start(){
        ServerSocket serverSocket;
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(port);
            while(true){
                System.out.println("waiting conn~");
                socket=serverSocket.accept();
                ServerThread serverThread = new ServerThread(socket);
                serverThread.start();
                st.add(serverThread);
                count++;
                if (logger.isDebugEnabled()){
                    logger.debug("client sizes" + count);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
